﻿/*
COPYRIGHT (C) 2008 EPISERVER AB

THIS FILE IS PART OF WIKIX.
WIKIX IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

WIKIX IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH WIKIX. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EPiServer.Core;

namespace EPiServer.Research.Wiki.Core
{
    public class EditManager
    {
        private static EditManager _instance;

        public static EditManager Instance
        {
            get
            {
                if (_instance == null) _instance = new EditManager();
                return _instance;
            }
        }

        private Dictionary<PageReference, EditData> _locks;
        private object _locallock = new object();



        public bool GetLock(PageReference id)
        {
            if (IsLocked(id))
            {
                if (_locks[id].User != EPiServer.Personalization.EPiServerProfile.Current.UserName) return false;
            }
            lock (_locallock)
            {
                EditData ed = new EditData();
                ed.Timestamp = DateTime.Now;
                ed.User = EPiServer.Personalization.EPiServerProfile.Current.UserName;
                _locks[id] = ed;
                //if (IsLocked(id)) return false;
                //_locks.Add(id, DateTime.Now);
            }
            return true;
        }

        public void ReleaseLock(PageReference id)
        {
            lock (_locallock)
            {
                if (_locks.ContainsKey(id)) _locks.Remove(id);
            }
        }

        public EPiServer.Personalization.EPiServerProfile WhoLocked(PageReference pr)
        {
            return EPiServer.Personalization.EPiServerProfile.Get(_locks[pr].User);
        }

        public PageDataCollection ListLockedPages()
        {
            var keys = _locks.Keys.ToList();
            keys.RemoveAll(pr => !IsLocked(pr));
            return new PageDataCollection(keys.Select(pr => DataFactory.Instance.GetPage(pr)).ToArray());
        }

        public bool IsLocked(PageReference id)
        {
            if (!_locks.ContainsKey(id)) return false;
            if (((TimeSpan)(DateTime.Now - _locks[id].Timestamp)).TotalSeconds < 30) return true;
            else
            {
                lock (_locallock)
                {
                    _locks.Remove(id);
                }
                return false;
            }
        }

        public EditManager()
        {
            _locks = new Dictionary<PageReference, EditData>();
        }
    }

    public struct EditData
    {
        public DateTime Timestamp;
        public string User;
    }
}
